package gengine.common.console;

import gengine.common.misc.BaseApplication;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;

/**
 * User: arseniy
 * Date: 23.12.11
 * Base class providing clientThread and server console
 */
public abstract class BaseConsole {
    protected List<String> storage = new ArrayList<String>();
    protected final Dictionary<String,CmdProcessor> registeredCommands = new Hashtable<String, CmdProcessor>();
    private int currentPosition =0;

    public String scrollDown() {
        currentPosition++;
        if (currentPosition>=storage.size()) {
            currentPosition=-1;
            return "";
        }
        return storage.get(currentPosition);
    }

    public String scrollUp() {
        currentPosition--;
        if (currentPosition<0) {
            currentPosition=storage.size();
            return "";
        }
        return storage.get(currentPosition);
    }

    public BaseConsole() {
        registeredCommands.put("quit",new CmdQuit());
    }

    /**
     * Processes input string and returns answer
     * @param text commandText
     * @return processor answer
     */
    public String processCommand(String text) {
        text = text.trim();
        if (storage.contains(text)) {
            storage.remove(text);
        }
        storage.add(text);
        currentPosition = storage.size()-1;
        String[] cmd = splitCommand(text);
        String cmdName = cmd[0];
        CmdProcessor processor = registeredCommands.get(cmdName);
        return processor==null?"Unknown command "+cmdName:processor.process(cmd);
    }

    private String[] splitCommand(String s) {
        List<String> cmd = new ArrayList<String>(4);
        //add last word to result
        s+=" ";
        String cur = "";
        boolean singleQuote=false;
        boolean doubleQuote = false;
        for (char c:s.toCharArray()) {
            switch (c) {
                case '"': 

                    if (singleQuote)
                        cur+=c;
                    else
                        doubleQuote = !doubleQuote;
                    break;
                case '\'': 

                    if (doubleQuote)
                        cur+=c;
                    else
                        singleQuote = !singleQuote;
                    break;
                case ' ': 
                    if (singleQuote || doubleQuote){
                        cur+=c;
                    } else {
                        if (cur!="")
                            cmd.add(cur);
                        cur ="";
                    }
                    break;
                default: cur+=c;
            }
        }
        return cmd.toArray(new String[0]);
    }

    public class CmdQuit implements CmdProcessor {

         @Override
         public String process(String[] args) {
             BaseApplication.getApplication().quit();
             return "";
         }
     }
}
